package BookStore.view;

import BookStore.dao.AdminDao;
import BookStore.dao.UserDao;
import BookStore.model.Admin;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class Aregister extends JFrame {
    private JPanel contentPane;
    private JTextField textField;
    private JPasswordField passwordField;
    private JPasswordField passwordField_1;


    public void result_3(){
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    Aregister frame = new Aregister();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Aregister() {
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(500, 250, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel label = new JLabel("注册管理员");
        label.setFont(new Font("Lucida Grande", Font.PLAIN, 17));
        label.setBounds(177, 22, 85, 32);
        contentPane.add(label);

        JLabel label_1 = new JLabel("管理员名");
        label_1.setBounds(92, 60, 61, 16);
        contentPane.add(label_1);

        JLabel label_2 = new JLabel("密  码");
        label_2.setBounds(92, 99, 34, 16);
        contentPane.add(label_2);

        JLabel label_3 = new JLabel("确认密码");
        label_3.setBounds(92, 139, 61, 16);
        contentPane.add(label_3);

        textField = new JTextField();
        textField.setBounds(165, 55, 130, 26);
        contentPane.add(textField);
        textField.setColumns(10);

        JButton button = new JButton("确定");
        button.setBounds(87, 184, 90, 29);
        contentPane.add(button);

        JButton button_1 = new JButton("退出");
        button_1.setBounds(245, 184, 90, 29);
        contentPane.add(button_1);

        passwordField = new JPasswordField();
        passwordField.setBounds(165, 94, 130, 26);
        contentPane.add(passwordField);

        passwordField_1 = new JPasswordField();
        passwordField_1.setBounds(165, 134, 130, 26);
        contentPane.add(passwordField_1);

        button_1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(-1);
            }
        });

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String t1 = textField.getText();
                String p1 = String.valueOf(passwordField.getPassword());
                String p2 = String.valueOf(passwordField_1.getPassword());

                if (t1.length()>0 && p1.length()>0){

                    if (p1.equals(p2)){
                        Admin admin = new Admin();
                        admin.setAdminName(t1);
                        admin.setAdminPsw(p1);

                        try {
                            new AdminDao(admin).register_1();
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }

                    }else {
                        JOptionPane.showMessageDialog(null,"密码不一致");
                        passwordField_1.setText("");
                    }

                }else {
                    JOptionPane.showMessageDialog(null,"管理员或密码不能为空");

                }
            }
        });
    }
}

